package com.jwds.digital.bios.business.service.rule.impl;

import com.jwds.digital.bios.business.core.constants.RuleCodeConstant;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchParameterReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.FiveBasicInformationReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.service.rule.DispatchRuleSortInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓的可用时间
 * @author 李志明
 */
@Slf4j
@Component
public class AvailableTime implements DispatchRuleSortInterface {
    @Override
    public String getRuleCode() {
        return RuleCodeConstant.AVAILABLE_TIME;
    }

    @Override
    public List<DispatchParameterReqDTO> sortProcess(List<DispatchParameterReqDTO> dataList) {
        // 库存除以放仓流量
        ArrayList<DispatchParameterReqDTO> parameterReqDTOArrayList = new ArrayList<>();
        ArrayList<FiveBasicInformationReqDTO> fiveBasicInformationReqDTOArrayList = new ArrayList<>();
        for (DispatchParameterReqDTO dispatchParameterReqDTO1 : dataList) {
            DispatchParameterReqDTO dispatchParameterReqDTO = new DispatchParameterReqDTO();
            for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO1 : dispatchParameterReqDTO1.getFlowList()) {
                FiveBasicInformationReqDTO fiveBasicInformationReqDTO = new FiveBasicInformationReqDTO();
                List<BasicStockBinRespVO> collect = fiveBasicInformationReqDTO1.getOtherSlotList().stream()
                        .sorted(Comparator.comparing((BasicStockBinRespVO b) -> {
                            if (b.getMaxSpeed().compareTo(BigDecimal.ZERO) == 0) {
                                return BigDecimal.ZERO;
                            }
                            return b.getCapacity().divide(b.getMaxSpeed(), 2, RoundingMode.HALF_UP);
                        }))
                        .collect(Collectors.toList());
                // 起点仓
                fiveBasicInformationReqDTO.setOtherSlotList(collect);
                // 流程路线
                fiveBasicInformationReqDTO.setFlow(fiveBasicInformationReqDTO1.getFlow());
                // 起点设备
                fiveBasicInformationReqDTO.setDeviceCodeList(fiveBasicInformationReqDTO1.getDeviceCodeList());
                // 起点默认值
                fiveBasicInformationReqDTO.setDeviceTypeDefault(fiveBasicInformationReqDTO1.getDeviceTypeDefault());
                // 起点默认位置
                fiveBasicInformationReqDTO.setOtherSlotDefault(fiveBasicInformationReqDTO1.getOtherSlotDefault());
                fiveBasicInformationReqDTOArrayList.add(fiveBasicInformationReqDTO);
            }
            // 终点仓
            dispatchParameterReqDTO.setSlot(dispatchParameterReqDTO1.getSlot());
            // 终点设备
            dispatchParameterReqDTO.setDeviceCodeList(dispatchParameterReqDTO1.getDeviceCodeList());
            // 终点设备默认值
            dispatchParameterReqDTO.setDeviceCodeDefault(dispatchParameterReqDTO1.getDeviceCodeDefault());
            // 监控状态
            dispatchParameterReqDTO.setState(dispatchParameterReqDTO1.getState());
            // 仓标识
            dispatchParameterReqDTO.setSlotIdentification(dispatchParameterReqDTO1.getSlotIdentification());
            dispatchParameterReqDTO.setFlowList(fiveBasicInformationReqDTOArrayList);
            parameterReqDTOArrayList.add(dispatchParameterReqDTO);
        }
        return parameterReqDTOArrayList;
    }
}
